<?php

namespace Home\Controller;

use Common\Service\Validator;
use Think\Controller;
use Think\Log;
use Common\Service\Curl;
use Common\Service\Weixin;

class UserController extends BaseController
{
    /**
     * 审核员，店长登录/绑定
     */
    public function login()
    {
        $mobile = I('mobile', '', 'trim');
        $password = I('password', '', 'trim');
        $openid = I('openid', '', 'trim');
        if (!Validator::checkPhone($mobile)) {
            $this->ajaxReturn(['status' => false, 'code' => 1001, 'msg' => '手机号错误']);
        }

        if ($password == '') {
            $this->ajaxReturn(['status' => false, 'code' => 1002, 'msg' => '请输入密码']);
        }

        if ($openid == '') {
            $this->ajaxReturn(['status' => false, 'code' => 1003, 'msg' => '非法登录']);
        }

        $audit = $this->chenkLogin('audit', $mobile, $password);
        $manager = $this->chenkLogin('store', $mobile, $password);
        if (!$audit && !$manager) {
            $this->ajaxReturn(['status' => false, 'code' => 1004, 'msg' => '账号密码错误']);
        }
        if ($audit) {
            $index = 1;
            $userInfo = $audit;
        }

        if ($manager) {
            $index = 2;
            $userInfo = $manager;
        }

        $tab = $this->getRoleTab()[$index];
        if ($userInfo) {
            if ($userInfo['openid'] == '') {
                M($tab)->where(['mobile' => $mobile])->save(['openid' => $openid]);
            } else {
                if ($openid != $userInfo['openid']) {
                    $this->ajaxReturn(['status' => false, 'code' => 1005, 'msg' => '非法请求，请联系管理员']);
                }
            }

            //前面更新了数据需要重新获取一次
            $userInfo = M($tab)->where(['id' => $userInfo['id']])->find();
            if ($tab == 'audit') {//审核员
                $data = [
                    'name' => $userInfo['name'],
                    'company_name' => $userInfo['company_name'],
                    //'user_id' => $userInfo['id'],
                    //'openid' => $userInfo['openid'],
                    //'user_type' => $index//角色
                ];
            } elseif ($tab == 'store') {//店长
                $company_name = $this->getStoreCompanyName($userInfo['id']);
                $data = [
                    'name' => $userInfo['manager_name'],
                    'company_name' => $company_name,
                    //'user_id' => $userInfo['id'],
                    //'openid' => $userInfo['openid'],
                    //'user_type' => $index//角色
                ];
            } else {
                $data = ['数据有误，叼毛'];
            }

            $file = M('files')->where(['file_id' => $userInfo['headimg']])->find();
            if ($file) {
                $file_path = C('DOMAIN') . $file['filepath'];
            } else {
                $file_path = $userInfo['headimg'];
            }

            $data['headimg'] = $file_path;

            $data['user_id'] = $userInfo['id'];
            $data['openid'] = $userInfo['openid'];
            $data['user_type'] = $index;
            $data['mobile'] = $userInfo['mobile'];
            $ret = ['status' => true, 'code' => 1000, 'msg' => '登陆成功', 'data' => $data];
            $this->ajaxReturn($ret);
        } else {
            $this->ajaxReturn(['status' => false, 'code' => 1006, 'msg' => '登录数据有误']);
        }
    }

    /**
     * 问题反馈,三个角色都可以使用
     */
    public function addFeedBack()
    {
        $arr = I('post.');
        $data['id'] = $arr['id'];
        $data['content'] = $arr['content'];
        $data['images'] = $arr['images'];
        $data['user_type'] = $this->getRoleTab()[$arr['user_type']];//audit,store,worker
        $data['add_time'] = time();

        $user = $this->checkUser($data['user_type'], $data['id']);
        if (!$user) {
            $this->ajaxReturn(['status' => false, 'code' => 1003, 'msg' => '用户信息有误', 'data' => []]);
        }

        if (!$data['content'] || !$data['user_type']) {
            $this->ajaxReturn(['status' => false, 'code' => 1002, 'msg' => '请填写完整数据再添加', 'data' => []]);
        }
        $ret = M('feedback')->add($data);
        if ($ret) {
            $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '添加问题反馈成功', 'data' => []]);
        } else {
            $this->ajaxReturn(['status' => false, 'code' => 1001, 'msg' => '服务器异常', 'data' => []]);
        }
    }
}